package com.victor.pomelo.controller;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.victor.pomelo.model.CopyOfPager;
import com.victor.pomelo.model.Group;
import com.victor.pomelo.model.Node;
import com.victor.pomelo.model.User;
import com.victor.pomelo.service.GroupService;
import com.victor.pomelo.service.UserService;

/**
 * 用户信息的增删改查操作
 * 
 * @author Victor
 *
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

	@Autowired
	private UserService userService;
	@Autowired
	private GroupService groupService;

	@RequestMapping("/index")
	public String toIndex(HttpServletRequest request, Model model) {

		return "user/index";
	}

	@ResponseBody
	@RequestMapping("/findgroup")
	public Map<String, List<Node>> findGroup(HttpServletRequest request,
			Model model) {
		Enumeration<String> paraNames = request.getParameterNames();
		for (Enumeration e = paraNames; e.hasMoreElements();) {
			String thisName = e.nextElement().toString();
			String thisValue = request.getParameter(thisName);
			System.out.println(thisName + "--------------" + thisValue);
		}

		Enumeration<String> attriNames = request.getAttributeNames();
		for (Enumeration e = paraNames; e.hasMoreElements();) {
			String thisName = e.nextElement().toString();
			String thisValue = (String) request.getAttribute(thisName);
			System.out.println(thisName + "==============" + thisValue);
		}
		String nodeid = request.getParameter("nodeid");
		String level = request.getParameter("n_level");
		List<Node> rows = new ArrayList<Node>();
		if (nodeid == null || "".equals(nodeid)) {
			rows = groupService.findChildren((long) 0, (level == null ? 0
					: Integer.valueOf(level) + 1));
		} else {
			rows = groupService.findChildren(Long.valueOf(nodeid),
					(level == null ? 0 : Integer.valueOf(level) + 1));
		}
		Map<String, List<Node>> rtndata = new HashMap<String, List<Node>>();
		rtndata.put("rows", rows);
		return rtndata;
	}

	@RequestMapping("/userlist")
	public String userList(HttpServletRequest request, Model model) {
		return "user/userlist";
	}

	@ResponseBody
	@RequestMapping("/finduserlist")
	public CopyOfPager finduserList(HttpServletRequest request, Model model) {
		Enumeration<String> paraNames = request.getParameterNames();
		for (Enumeration e = paraNames; e.hasMoreElements();) {
			String thisName = e.nextElement().toString();
			String thisValue = request.getParameter(thisName);
			System.out.println(thisName + "--------------" + thisValue);
		}

		Enumeration<String> attriNames = request.getAttributeNames();
		for (Enumeration e = paraNames; e.hasMoreElements();) {
			String thisName = e.nextElement().toString();
			String thisValue = (String) request.getAttribute(thisName);
			System.out.println(thisName + "==============" + thisValue);
		}

		String page = request.getParameter("page"); // 第几页
		String limit = request.getParameter("rows"); // 每页显示多少行
		
		
		

		String nodeid = request.getParameter("nodeid");
		
		
		
		
		// 分页查询数据
		List<User> rows = userService.findAll1(new Long(Integer.valueOf(page)
				* Integer.valueOf(limit)), new Long(Integer.valueOf(limit)));
		CopyOfPager<User> cp = new CopyOfPager<User>();
		cp.setPage(Integer.valueOf(page));
		cp.setRecords(new Long(1000));
		cp.setRows(rows);
		cp.setTotal(100);
		String json = new JSONArray().fromObject(cp).toString();
		System.err.println("发送到页面的json: " + json);

		return cp;
	}

	@RequestMapping("/setuserlist")
	public String setUserList(HttpServletRequest request, Model model) {
		Enumeration<String> paraNames = request.getParameterNames();
		for (Enumeration e = paraNames; e.hasMoreElements();) {
			String thisName = e.nextElement().toString();
			String thisValue = request.getParameter(thisName);
			System.out.println(thisName + "--------------" + thisValue);
		}

		Enumeration<String> attriNames = request.getAttributeNames();
		for (Enumeration e = paraNames; e.hasMoreElements();) {
			String thisName = e.nextElement().toString();
			String thisValue = (String) request.getAttribute(thisName);
			System.out.println(thisName + "==============" + thisValue);
		}
		model.addAttribute("success", "操作成功");
		return "success";
	}

}
